<?php

	function getPipePos($n, $line)
	{
		$pos = 0;
		for ($i = 0; $i < $n; $i++)
		{
			$pos = strpos($line, "|", $pos + 1);
		}
		return $pos;
	}

	function getRank($line)
	{
		$length = getPipePos(1, $line);
		return substr($line, 0, $length);
	}

	function getField($s, $e, $line)
	{
		$start = getPipePos($s, $line) + 1;
		$length = getPipePos($e, $line) - $start;
		return substr($line, $start, $length);
	}

	function getAggregatedData($yahooline, $espnline)
	{	
		$espnRank = getRank($espnline);	
		$espnTeam = getField(2, 3, $espnline);
		$espnGamesMins = getField(4, 6, $espnline);

		//8-15
		//|3PM|REB|AST|STL|BLK|TO|PTS|
		$espn3pm = getField(8, 9, $espnline);
		$espnReb = getField(9, 10, $espnline);
		$espnAst = getField(10, 11, $espnline);
		$espnStl = getField(11, 12, $espnline);
		$espnBlk = getField(12, 13, $espnline);
		$espnTo = getField(13, 14, $espnline);
		$espnPts = getField(14, 15, $espnline);

		$aggrData = substr_replace($yahooline, "|" . $espnRank, getPipePos(1, $yahooline), 0);
		$aggrData = substr_replace($aggrData, "|" . $espnTeam, getPipePos(3, $aggrData), 0);
		$aggrData = substr_replace($aggrData, "|" . $espnGamesMins, getPipePos(5, $aggrData), 0);

		// |l3PTM|lPTS|lREB|lAST|lST|lBLK|lTO
		$aggrData .= $espn3pm."|".$espnPts."|".$espnReb."|".$espnAst."|".$espnStl."|".$espnBlk."|".$espnTo;


		return $aggrData;
	}

	$espnfilename = "espnrankings.data";
	$espnfile = file_get_contents("out/" . $espnfilename);
	$espnlines = explode("\n", $espnfile);

	$yahoofilename = "yahoorankings.data";
	$yahoofile = file_get_contents("out/" . $yahoofilename);
	$yahoolines = explode("\n", $yahoofile);


	$AGGREGATED_LIST = "Y!|E|NAME|TEAM|POS|GMS|MIN|lFGM|lFGA|lFG%|lFTM|lFTA|lFT%|l3PTM|lPTS|lREB|lAST|lST|lBLK|lTO|pFGM|pFGA|pFG%|pFTM|pFTA|pFT%|p3PTM|pPTS|pREB|pAST|pST|pBLK|pTO|a3PTM|aPTS|aREB|aAST|aST|aBLK|aTO";
	$AGGREGATED_PLAYERDATA = "";
	$NOT_IN_ESPN_LIST = "";
	$NOT_IN_ESPN_LIST_COUNT = 0;
	$i = 0;
	foreach($yahoolines as $yahooline)
	{
		if ($i > 0)
		{
			$name = getField(1, 2, $yahooline);
			$found = false;
			foreach($espnlines as $espnline)
			{
				$posInEspn = strpos($espnline, $name);
				if ($posInEspn !== false)
				{
					$AGGREGATED_PLAYERDATA = getAggregatedData($yahooline, $espnline);
					$AGGREGATED_LIST .= "\n" . $AGGREGATED_PLAYERDATA;

					$found = true;
					break;
				}
			}
			if (!$found)
			{
				$NOT_IN_ESPN_LIST .= $name . "<br>";
				$NOT_IN_ESPN_LIST_COUNT++;
			}
		}
		$i++;
	}

	if (file_put_contents("out/espnyahooaggr.data", $AGGREGATED_LIST) !== false)
	{	
		echo "<h3>Successfully aggregated out/$espnfilename to out/$yahoofilename.</h3>" .
			"<h3>Processed data is at out/espnyahooaggr.data.</h3>";
		echo "<h6>Not Found in ESPN (".$NOT_IN_ESPN_LIST_COUNT.")</h6>";
		echo $NOT_IN_ESPN_LIST;
		echo "<iframe src='out/espnyahooaggr.data' width='100%' height='80%'>";

	
	}
	else
	{
		echo "<h3>Error processing raw data.</h3>";
	}
?>